﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fa.Kanbox.API.Demo
{
    public partial class FSelectFolderDialog : Form
    {
        public FSelectFolderDialog()
        {
            InitializeComponent();
        }
        private void FillTree()
        {
            treeView1.Nodes.Clear();
            FillTree(treeView1.Nodes, "/");
        }
        private void FillTree(TreeNodeCollection nodes,string path)
        {
            var list = MyGlobal.ApiClient.GetListInfo(path);
            foreach (var item in list.Contents.Where(m => m.IsFolder)) {
                var node = new TreeNode(Path.GetFileName(item.FullPath))
                {
                    Tag = item
                };
                nodes.Add(node);
                this.FillTree(node.Nodes, item.FullPath);
            }
        }
        public KbListContentInfo FolderInfo { get; set; }

        private void FSelectFolder_Shown(object sender, EventArgs e)
        {
            FillTree();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var selectedNode = treeView1.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show("请选择一个目录");
                return;
            }
            this.FolderInfo = selectedNode.Tag as KbListContentInfo;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
